package cn.mulanbay.schedule.impl;

import cn.mulanbay.schedule.SchedulePersistentProcessor;
import cn.mulanbay.schedule.domain.TaskLog;
import cn.mulanbay.schedule.domain.TaskServer;
import cn.mulanbay.schedule.domain.TaskTrigger;
import cn.mulanbay.schedule.enums.TriggerStatus;
import cn.mulanbay.schedule.mapper.TaskLogMapper;
import cn.mulanbay.schedule.mapper.TaskServerMapper;
import cn.mulanbay.schedule.mapper.TaskTriggerMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

/**
 * 调度持久层的hibernate实现
 *
 * @author fenghong
 * @create 2022-01-16 10:00
 */
public class MybatisPersistentProcessor implements SchedulePersistentProcessor {

    @Autowired
    protected TaskTriggerMapper taskTriggerMapper;

    @Autowired
    protected TaskLogMapper taskLogMapper;

    @Autowired
    protected TaskServerMapper taskServerMapper;

    /**
     * 更新新的调度执行后的调度
     *
     * @param taskTrigger
     */
    @Override
    public void updateTaskTriggerForNewJob(TaskTrigger taskTrigger) {
        TaskTrigger dbBean = taskTriggerMapper.selectById(taskTrigger.getId());
        dbBean.setTotalCount(taskTrigger.getTotalCount());
        dbBean.setFailCount(taskTrigger.getFailCount());
        dbBean.setLastExecuteResult(taskTrigger.getLastExecuteResult());
        dbBean.setLastExecuteTime(taskTrigger.getLastExecuteTime());
        dbBean.setNextExecuteTime(taskTrigger.getNextExecuteTime());
        taskTriggerMapper.updateById(dbBean);
    }

    /**
     * 保存调度日志
     * @param taskLog
     */
    @Override
    public void saveTaskLog(TaskLog taskLog) {
        taskLog.setTaskTriggerId(taskLog.getTaskTrigger().getId());
        taskLogMapper.insert(taskLog);
    }

    /**
     * 获取调度日志
     * @param logId
     * @return
     */
    @Override
    public TaskLog selectTaskLog(Long logId) {
        TaskLog log = taskLogMapper.selectById(logId);
        if(log!=null){
            TaskTrigger tt = this.selectTaskTrigger(log.getTaskTriggerId());
            log.setTaskTrigger(tt);
        }
        return log;
    }

    /**
     * 获取调度器
     * @param triggerId
     * @return
     */
    @Override
    public TaskTrigger selectTaskTrigger(Long triggerId) {
        return taskTriggerMapper.selectById(triggerId);
    }

    /**
     * 更新调度日志
     * @param taskLog
     */
    @Override
    public void updateTaskLog(TaskLog taskLog) {
        taskLog.setTaskTriggerId(taskLog.getTaskTrigger().getId());
        taskLogMapper.updateById(taskLog);
    }

    /**
     * 更新重做后的调度执行后的调度
     * @param taskTrigger
     */
    @Override
    public void updateTaskTriggerForRedoJob(TaskTrigger taskTrigger) {
        this.updateTaskTriggerForNewJob(taskTrigger);
    }

    /**
     * 更新调度状态
     * @param triggerId
     * @param status
     */
    @Override
    public void updateTaskTriggerStatus(Long triggerId, TriggerStatus status) {
        taskTriggerMapper.updateTaskTriggerStatus(triggerId,status);
    }

    /**
     * 获取有效的调度器
     * @param deployId
     * @param supportDistri
     * @return
     */
    @Override
    public List<TaskTrigger> getActiveTaskTrigger(String deployId, boolean supportDistri) {
        return taskTriggerMapper.getActiveTaskTrigger(deployId,supportDistri);
    }

    /**
     * 检查调度日志是否存在
     * @param taskTriggerId
     * @param bussDate
     * @return
     */
    @Override
    public boolean isTaskLogExit(Long taskTriggerId, Date bussDate) {
        long n = taskLogMapper.getCountByDate(taskTriggerId,bussDate);
        return n>0 ? true : false;
    }

    @Override
    public boolean isTaskLogExit(String scheduleIdentityId) {
        long n = taskLogMapper.getCountByIdentityId(scheduleIdentityId);
        return n>0 ? true : false;
    }

    /**
     * 获取自动重做的调度任务
     * @param deployId
     * @param supportDistri
     * @param startDate 最小开始时间
     * @param endDate 最大开始时间
     * @return
     */
    @Override
    public List<TaskLog> getAutoRedoTaskLogs(String deployId, boolean supportDistri, Date startDate,Date endDate) {
        return taskLogMapper.getAutoRedoTaskLogList(deployId,supportDistri, startDate,endDate);
    }

    /**
     * 更新获取保存
     * @param taskServer
     */
    @Override
    public void updateTaskServer(TaskServer taskServer) {
        if(taskServer.getId()==null){
            taskServerMapper.insert(taskServer);
        }else{
            taskServerMapper.updateById(taskServer);
        }
    }

    @Override
    public TaskServer selectTaskServer(String deployId) {
        return taskServerMapper.getTaskServerByDeployId(deployId);
    }
}
